package top.lyc829.zzl_android.ui.thing.travels;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.TravelsSimpleBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class TravelsViewModel extends ViewModel {
    private MutableLiveData<List<TravelsSimpleBean>> travelsLiveData;

    public MutableLiveData<List<TravelsSimpleBean>> getTravelsLiveData() {
        if (travelsLiveData == null) {
            travelsLiveData = new MutableLiveData<>();
        }
        return travelsLiveData;
    }

    public void loadTravelItems(int page) {
        Network.loadTravels(page, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<List<TravelsSimpleBean>> resultBean = JsonUtilNew.fromJsonArray(
                        res.body().string(), TravelsSimpleBean.class);
                if (resultBean.getCode().equals(200)) {
                    getTravelsLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }



}